<template>
  <div class="app-container">
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-row>
        <el-col :span="6">
          <el-form-item label="模版名字" prop="name">
            <el-input v-model="form.name" placeholder="请输入药品模版名字" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="模版目的" prop="aim">
            <el-input v-model="form.aim" placeholder="请输入药品模版目的" />
          </el-form-item>
        </el-col>
        <el-col :span="6" v-if="false">
          <el-form-item label="模版编码" prop="code">
            <el-input v-model="form.code" placeholder="请输入药品模版编码" disabled="disabled"/>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="草药医嘱" prop="medicalAdvice">
            <el-input v-model="form.medicalAdvice" placeholder="请输入草药：医嘱" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="草药治法" prop="therapy">
            <el-input v-model="form.therapy" placeholder="请输入草药：治法" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="6">
          <el-form-item label="治法细节" prop="therapyDetails">
            <el-input v-model="form.therapyDetails" placeholder="请输入草药：治法细节" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="草药付数" prop="pairNum">
            <el-input v-model="form.pairNum" placeholder="请输入草药：付数" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="草药频次" prop="frequency">
            <el-input v-model="form.frequency" placeholder="请输入草药：频次"/>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="范围类型">
            <el-select v-model="form.scope" placeholder="请选择药品模版范围类型">
              <el-option
                v-for="dict in scopeOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="parseInt(dict.dictValue)"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="6">
          <el-button style="float: left;margin: 10px" type="info" @click="addDrug_model">添加明细</el-button>
          <el-button style="float: left;margin: 10px" @click="handleDelete" type="info" icon="el-icon-error">删除明细</el-button>　
        </el-col>
        <el-col :span="18" style="text-align: right">
          <el-button type="primary" @click="submitForm">修改</el-button>
          <el-button @click="cancel">返回</el-button>
        </el-col>
      </el-row>
    </el-form>
    <el-table v-loading="loading" :data="medicine_model_itemList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="药品名" width="220" align="center" prop="drugName" />
      <el-table-column label="药品规格" align="center" prop="drugFormat" />
      <el-table-column label="单价(元)" align="center" prop="drugPrice" />
      <el-table-column label="药品数量" align="center" prop="num" />
      <el-table-column label="总价" align="center">
        <template slot-scope="scope">
          {{scope.row.usageNum*scope.row.drugPrice}}
        </template>
      </el-table-column>
      <el-table-column label="频次" align="center" prop="frequency" />
      <el-table-column label="天数" align="center" prop="days" />
      <el-table-column label="用量" align="center" prop="usageNum" />
      <el-table-column label="单位" align="center" prop="drugFormat" />
      <el-table-column label="使用建议" align="center" prop="modelId" />
      <el-table-column
        label="操作"
        align="center"
        width="80"
        prop="del"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:user:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 添加或修改草药模版项对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="状态">
          <el-radio-group v-model="form.status">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="说明" prop="footnote">
          <el-input v-model="form.footnote" placeholder="请输入说明" />
        </el-form-item>
        <el-form-item label="药品id" prop="drugId">
          <el-input v-model="form.drugId" placeholder="请输入药品id" />
        </el-form-item>
        <el-form-item label="用量" prop="usageNum">
          <el-input v-model="form.usageNum" placeholder="请输入用量" />
        </el-form-item>
        <el-form-item label="用量单位" prop="usageNumUnit">
          <el-input v-model="form.usageNumUnit" placeholder="请输入用量单位" />
        </el-form-item>
        <el-form-item label="药品模板id" prop="modelId">
          <el-input v-model="form.modelId" placeholder="请输入药品模板id" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!--模板明细添加-->
    <el-dialog :title="title" :visible.sync="addHerbalModelItemOpen" width="90%">
      <addDrugModel @closeAddDrugModel="addDrugModelClose" :modelId="modelId" style="margin-top: -40px"></addDrugModel>
    </el-dialog>
  </div>
</template>

<script>

import {delMedicine_model_item,addMedicine_model_item,updateMedicine_model_item,exportMedicine_model_item,getMedicine_model_item_by_modelId} from "@/api/dms/medicine_model_item";
import { listDrug_model, getDrug_model, delDrug_model, addDrug_model, updateDrug_model, exportDrug_model } from "@/api/dms/drug_model";
import addDrugModel from "./addDrugModel";

export default {
  components:{
    addDrugModel
  },
  name: "Herbal_model_item",
  data() {
    return {
      // 药品模版范围类型 scope 0个人 1科室  2全院字典
      scopeOptions: [],
      //药品类型 type 1草药 2中成药 3西药
      typeOptions: [],
      //药品模板状态
      statusOptions: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 草药模版项表格数据
      medicine_model_itemList:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        status: null,
        footnote: null,
        drugId: null,
        usageNum: null,
        usageNumUnit: null,
        modelId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      //模板Id
      modelId:0,
      //抓药弹窗
      addHerbalModelItemOpen:false
    };
  },
  created() {
    this.getList();
    this.updateModelInfo(this.$route.query.id);
    this.getDicts("model_scope").then(response => {
      this.scopeOptions = response.data;
    });
    this.getDicts("drug_type").then(response => {
      this.typeOptions = response.data;
    });
    this.getDicts("sys_normal_disable").then(response => {
      this.statusOptions = response.data;
    });
  },
  methods: {
    /** 查询草药模版项列表 */
    getList() {
      this.loading = true;
      this.modelId=this.$route.query.id;
      getMedicine_model_item_by_modelId(this.$route.query.id).then(response => {
        this.medicine_model_itemList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.$router.push("/template/medicine_model_item");
      // 销毁组件，避免通过vue-router再次进入时，仍是上次的history缓存的状态
      this.$destroy(true);
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        status: 0,
        footnote: null,
        drugId: null,
        usageNum: null,
        usageNumUnit: null,
        modelId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加草药模版项";
    },
    /** 修改模板明细 */
    updateModelInfo(id) {
      this.reset();
      getDrug_model(id).then(response => {
        this.form = response.data;
        this.title = "修改药品模版";
      });

    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateDrug_model(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.$router.push("/template/medicine_model_item");
                // 销毁组件，避免通过vue-router再次进入时，仍是上次的history缓存的状态
                this.$destroy(true);
              }
            });
          } else {
            addMedicine_model_item(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      if(ids.length>0||ids>0){
        this.$confirm('是否确认删除草药模版项编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delMedicine_model_item(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
      }
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有草药模版项数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportMedicine_model_item(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    },
    /**药品加入模板页面*/
    addDrug_model(){
      this.addHerbalModelItemOpen=true;
      this.title='草药模板明细添加'
    },
    /**关闭明细弹窗*/
    addDrugModelClose(){
      this.addHerbalModelItemOpen=false;
      this.getList();
    }
  }
};
</script>
